package models

import (
	"github.com/beego/beego/v2/client/orm"
	"time"
)

type SysLogAdmin struct {
	Id            int64      `orm:"column(id);unique;auto;" json:"id"`
	BusinessType  int        `orm:"column(business_type);description(业务类型（0其它 1新增 2修改 3删除)" json:"businessType"`
	Method        string     `orm:"column(method);description(方法名称)" json:"method"`
	RequestMethod string     `orm:"column(request_method);description(请求方式)" json:"requestMethod"`
	OperatorType  int        `orm:"column(operator_type);description(操作类别（0其它 1后台用户 2手机端用户)" json:"operatorType"`
	OperName      string     `orm:"column(oper_name);description(操作人员)" json:"operName"`
	DeptName      string     `orm:"column(dept_name);description(部门名称)" json:"deptName"`
	OperUrl       string     `orm:"column(oper_url);description(请求URL)" json:"operUrl"`
	OperIp        string     `orm:"column(oper_ip);description(主机地址)" json:"operIp"`
	OperParam     string     `orm:"column(oper_param);description(请求参数)" json:"operParam"`
	JsonResult    string     `orm:"column(json_result);description(返回参数)" json:"jsonResult"`
	Status        int        `orm:"column(status);description(操作状态（0正常 1异常）)" json:"status"`
	ErrorMsg      string     `orm:"column(error_msg);description(错误消息)" json:"errorMsg"`
	OperTime      *time.Time `orm:"column(oper_time);description(操作时间)" json:"operTime"`
	CostTime      int64      `orm:"column(cost_time);description(消耗时间)" json:"costTime"`
	OperUserId    int64      `orm:"column(oper_user_id);description(操作人id)" json:"operUserId"`
}

func init() {
	orm.RegisterModel(new(SysLogAdmin))
}

// SearchField 定义模型的可搜索字段
func (*SysLogAdmin) SearchField() []string {
	return []string{"status"}
}

// WhereField 定义模型可作为条件的字段
func (*SysLogAdmin) WhereField() []string {
	return []string{"id", "status"}
}

// SimpleField 简略查询字段
func (*SysLogAdmin) SimpleField() []string {
	return []string{"id"}
}

func (m *SysLogAdmin) TableName() string {
	return "sys_log_admin"
}

// AddSysLogAdmin insert a new SysLogAdmin into database and returns
// last inserted Id on success.
func AddSysLogAdmin(m *SysLogAdmin) (id int64, err error) {
	o := orm.NewOrm()
	id, err = o.Insert(m)
	return
}

// GetSysLogAdminById retrieves SysLogAdmin by Id. Returns error if
// Id doesn't exist
func GetSysLogAdminById(id int64) (v *SysLogAdmin, err error) {
	o := orm.NewOrm()
	v = &SysLogAdmin{}
	if err = o.QueryTable(new(SysLogAdmin)).Filter("id", id).RelatedSel().One(v); err == nil {
		return v, nil
	}
	return nil, err
}
